#include "lcd12864.c"

void display_init()
{
    lcd_init();
    lcd_clear_screen();
}

void display_clear()
{
    lcd_clear_screen();
}

void display_num(uchar page, uchar col, uchar num)
{
    lcd_display_bytes_in_page(page, col, &NUM_CHARS + num * 4, 4);
}

void display_score_chars()
{
    lcd_display_bytes_in_page(0, 3, SCORE_CHARS, sizeof(SCORE_CHARS));
}

void display_score_num()
{
    uchar page = 1;
    uchar col = 3;

    // 高位
    display_num(page, col, game_score / 10000);
    display_num(page, col += 5, (game_score % 10000) / 1000);
    display_num(page, col += 5, (game_score % 1000) / 100);
    display_num(page, col += 5, (game_score % 100) / 10);
    display_num(page, col += 5, game_score % 10);
}

void display_speed_chars_playing()
{
    lcd_display_bytes_in_page(3, 3, SPEED_CHARS, sizeof(SPEED_CHARS));
}

void display_speed_num_playing()
{
    display_num(4, 13, game_speed);
}

/**
 *  显示游戏区域的两条线，两条线中间的宽度是40像素
 */
void display_game_boundary()
{
    unsigned char i;
    for (i = 0; i < 8; i++)
    {
        lcd_display_one_byte_in_page(i, 30, 0xff);
        lcd_display_one_byte_in_page(i, 71, 0xff);
    }
}

/**
 * 上下两个盒子，转换成4byte显示数据，每个盒子对应4x4像素
 */
void display_data_fill_two_box(uchar display_data[], bit top_box, bit bottom_box)
{
    uchar temp = 0x00;
    if (top_box == 1)
    {
        // LCD显示的时候低位在上面
        temp |= 0x0F;
    }

    if (bottom_box)
    {
        temp |= 0xF0;
    }

    display_data[0] = temp;
    display_data[1] = 0xbb & temp;
    display_data[2] = 0xdd & temp;
    display_data[3] = temp;
}

void display_game_zone()
{
    uchar two_box_display_data[] = {0x00, 0x00, 0x00, 0x00};
    uchar i, j;
    for (i = 0; i < 8; i++)
    {
        for (j = 0; j < 10; j++)
        {
            display_data_fill_two_box(two_box_display_data, (GAME_ZONE[2 * i] >> (12 - j)) & 0x0001 == 1,
                                      (GAME_ZONE[2 * i + 1] >> (12 - j)) & 0x0001 == 1);
            lcd_display_bytes_in_page(i, 30 + 1 + j * 4, two_box_display_data, sizeof(two_box_display_data));
        }
    }
}

void display_start_pic()
{
    uchar page;
    for (page = 2; page < 6; page++)
    {
        lcd_display_bytes_in_page(page, 0, &START_CHARS[(page - 2) * 128], 128);
    }
}

void display_game_over()
{
    uchar page;
    for (page = 0; page < 8; page++)
    {
        lcd_display_bytes_in_page(page, 0, &GAME_OVER_CHARS[page * 128], 128);
    }
}

void display_speed_chars_choosing()
{
    lcd_display_bytes_in_page(3, 52, SPEED_CHARS, sizeof(SPEED_CHARS));
}

void display_speed_num_choosing()
{
    display_num(4, 62, game_speed);
}

void display_speed_chose()
{
    lcd_display_same_bytes_in_page(0, 0, 0xFF, 128);
    lcd_display_same_bytes_in_page(7, 0, 0xFF, 128);
    display_speed_chars_choosing();
    lcd_display_bytes_in_page(4, 57, LEFT_ARROW, sizeof(LEFT_ARROW));
    lcd_display_bytes_in_page(4, 67, RIGHT_ARROW, sizeof(RIGHT_ARROW));
    display_speed_num_choosing();
}

void display_icon_words()
{
    uchar i;
    // 一共2个汉字，每个汉字是16x16像素，
    // 前面16byte放第一页，后面16byte放第二页
    for (i = 0; i < 2; i++)
    {
        lcd_display_bytes_in_page(1, 85 + i * 16, &ICON_WORDS[i * 32], 16);
        lcd_display_bytes_in_page(2, 85 + i * 16, &ICON_WORDS[i * 32 + 16], 16);
    }

    // 一共5个英文字母，每个字母是8x16像素，
    // 分两页展示
    for (i = 0; i < 6; i++)
    {
        lcd_display_bytes_in_page(3, 77 + i * 8, &RIGHT_WORDS_EN[i * 16], 8);
        lcd_display_bytes_in_page(4, 77 + i * 8, &RIGHT_WORDS_EN[i * 16 + 8], 8);
    }
}

void display_pause_chars()
{
    uchar i;
    for (i = 0; i < 5; i++)
    {
        lcd_display_bytes_in_page(6, 81 + i * 8, &PAUSE_CHARS[i * 16], 8);
        lcd_display_bytes_in_page(7, 81 + i * 8, &PAUSE_CHARS[i * 16 + 8], 8);
    }
}

void display_hide_pause_chars()
{
    lcd_display_same_bytes_in_page(6, 81, 0x00, 40);
    lcd_display_same_bytes_in_page(7, 81, 0x00, 40);
}
